package io.kimmking.statemachine02;

import io.kimmking.statemachine02.squirrel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.squirrelframework.foundation.fsm.StateMachineBuilderFactory;
import org.squirrelframework.foundation.fsm.UntypedStateMachine;
import org.squirrelframework.foundation.fsm.UntypedStateMachineBuilder;

import java.util.Date;

@SpringBootApplication
public class Statemachine02Application implements ApplicationRunner {

	public static void main(String[] args) {
		SpringApplication.run(Statemachine02Application.class, args);
	}

	@Autowired
	SubmitOrderStateMachine fsm;

	@Override
	public void run(ApplicationArguments args) throws Exception {
		System.out.println("Run Squirrel StateMachine ......");
		fsm.start();

		OrderDTO orderDTO = new OrderDTO();
		orderDTO.setOrderId(10000L);
		orderDTO.setUserId(1L);
		orderDTO.setItemId(100L);
		orderDTO.setCreateTime(new Date());
		orderDTO.setOrderState(OrderState.INIT);
		OrderContext orderContext = new OrderContext(orderDTO);
		fsm.fire(OrderEvent.SUBMIT_ORDER, orderContext);

		fsm.fire(OrderEvent.PAY,orderContext);

		fsm.fire(OrderEvent.PART_SEND,orderContext);

		fsm.fire(OrderEvent.SEND,orderContext);

		fsm.fire(OrderEvent.COMPLETE,orderContext);
	}
}
